<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/vhuichen.ico">
  <link rel="mask-icon" href="/vhuichen.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"vhuichen.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.13.0","exturl":false,"sidebar":{"position":"left","display":"display","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="前言项目有个需求，比较两个字符串的大小，百度一下发现这个方法，记录下来！ 代码方法名    1- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;  代码实现：   123456789101112131415161718192021222324252627282930313233">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS基础之理解NSNumericSearch">
<meta property="og:url" content="https://vhuichen.github.io/2017/05/2017/05/iOS%E5%9F%BA%E7%A1%80%E4%B9%8B%E7%90%86%E8%A7%A3NSNumericSearch/index.html">
<meta property="og:site_name" content="vhuichen">
<meta property="og:description" content="前言项目有个需求，比较两个字符串的大小，百度一下发现这个方法，记录下来！ 代码方法名    1- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;  代码实现：   123456789101112131415161718192021222324252627282930313233">
<meta property="og:locale">
<meta property="article:published_time" content="2017-05-19T13:51:32.000Z">
<meta property="article:modified_time" content="2018-07-01T08:06:50.345Z">
<meta property="article:author" content="vhuichen">
<meta property="article:tag" content="NSNumericSearch">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://vhuichen.github.io/2017/05/2017/05/iOS%E5%9F%BA%E7%A1%80%E4%B9%8B%E7%90%86%E8%A7%A3NSNumericSearch/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-Hans","comments":true,"permalink":"https://vhuichen.github.io/2017/05/2017/05/iOS%E5%9F%BA%E7%A1%80%E4%B9%8B%E7%90%86%E8%A7%A3NSNumericSearch/","path":"2017/05/2017/05/iOS基础之理解NSNumericSearch/","title":"iOS基础之理解NSNumericSearch"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>iOS基础之理解NSNumericSearch | vhuichen</title>
  






  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">vhuichen</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">记录、分享</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section">Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section">About</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81"><span class="nav-number">2.</span> <span class="nav-text">代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">3.</span> <span class="nav-text">总结</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="vhuichen"
      src="/images/vhuichen.jpg">
  <p class="site-author-name" itemprop="name">vhuichen</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">78</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">86</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://vhuichen.github.io/2017/05/2017/05/iOS%E5%9F%BA%E7%A1%80%E4%B9%8B%E7%90%86%E8%A7%A3NSNumericSearch/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/vhuichen.jpg">
      <meta itemprop="name" content="vhuichen">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vhuichen">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="iOS基础之理解NSNumericSearch | vhuichen">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          iOS基础之理解NSNumericSearch
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2017-05-19 21:51:32" itemprop="dateCreated datePublished" datetime="2017-05-19T21:51:32+08:00">2017-05-19</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2018-07-01 16:06:50" itemprop="dateModified" datetime="2018-07-01T16:06:50+08:00">2018-07-01</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/iOS%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">iOS基础</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>项目有个需求，比较两个字符串的大小，百度一下发现这个方法，记录下来！</p>
<h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><p>方法名   </p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="built_in">NSComparisonResult</span>)compare:(<span class="built_in">NSString</span> *)string options:(<span class="built_in">NSStringCompareOptions</span>)mask;</span><br></pre></td></tr></table></figure>

<p>代码实现：  </p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="built_in">NSString</span> *str0 = <span class="string">@&quot;F0FF23FF&quot;</span>;</span><br><span class="line"><span class="built_in">NSString</span> *str1 = <span class="string">@&quot;F0FF023FF&quot;</span>;</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;%@ compare %@ == %zd&quot;</span>, str0, str1, [str0 compare:str1 options:<span class="built_in">NSNumericSearch</span>]);</span><br><span class="line"><span class="comment">//log : F0FF23FF compare F0FF023FF == 0</span></span><br><span class="line"><span class="comment">//理解：compare方法内部可以理解为把两个字符串拆分成</span></span><br><span class="line"><span class="comment">//F0FF23FF  --&gt; F 0 FF  23 FF</span></span><br><span class="line"><span class="comment">//F0FF023FF --&gt; F 0 FF 023 FF</span></span><br><span class="line"><span class="comment">//结果等于:0(两者相等)</span></span><br><span class="line"><span class="comment">//说明这个方法会将相邻的数字当成一个整体</span></span><br><span class="line"></span><br><span class="line">str0 = <span class="string">@&quot;E0FF23FF&quot;</span>;</span><br><span class="line">str1 = <span class="string">@&quot;F0FF23FF&quot;</span>;</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;%@ compare %@ == %zd&quot;</span>, str0, str1, [str0 compare:str1 options:<span class="built_in">NSNumericSearch</span>]);</span><br><span class="line"><span class="comment">//log : E0FF23FF compare F0FF23FF == -1</span></span><br><span class="line"><span class="comment">//理解：compare方法内部可以理解为把两个字符串拆分成</span></span><br><span class="line"><span class="comment">//E0FF23FF --&gt; E 0 FF 23 FF</span></span><br><span class="line"><span class="comment">//F0FF23FF --&gt; F 0 FF 23 FF</span></span><br><span class="line"><span class="comment">// E &lt; F , 结果等于:-1(前者小于后者)</span></span><br><span class="line"><span class="comment">//说明非数字也会进行比较</span></span><br><span class="line"></span><br><span class="line">str0 = <span class="string">@&quot;EF0FF23FF&quot;</span>;</span><br><span class="line">str1 = <span class="string">@&quot;F0FF23FF&quot;</span>;</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;%@ compare %@ == %zd&quot;</span>, str0, str1, [str0 compare:str1 options:<span class="built_in">NSNumericSearch</span>]);</span><br><span class="line"><span class="comment">//log : EF0FF23FF compare F0FF23FF == -1</span></span><br><span class="line"><span class="comment">//理解：compare方法内部可以理解为把两个字符串拆分成</span></span><br><span class="line"><span class="comment">//EF0FF23FF --&gt; EF 0 FF 23 FF</span></span><br><span class="line"><span class="comment">//F0FF23FF  --&gt; F  0 FF 23 FF</span></span><br><span class="line"><span class="comment">//从左到右进行比较，E &lt; F , 结果等于:-1(前者小于后者)</span></span><br><span class="line"><span class="comment">//说明此处是将&quot;EF&quot;中的&quot;E&quot; 和 &quot;F&quot; 比较，非数字不当成一个整体</span></span><br><span class="line"></span><br><span class="line">str0 = <span class="string">@&quot;E1FF23FF&quot;</span>;</span><br><span class="line">str1 = <span class="string">@&quot;F0FF23FF&quot;</span>;</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;%@ compare %@ == %zd&quot;</span>, str0, str1, [str0 compare:str1 options:<span class="built_in">NSNumericSearch</span>]);</span><br><span class="line"><span class="comment">//log : E1FF23FF compare F0FF23FF == -1</span></span><br><span class="line"><span class="comment">//理解：compare方法内部可以理解为把两个字符串拆分成</span></span><br><span class="line"><span class="comment">//E0FF23FF --&gt; E 1 FF 23 FF</span></span><br><span class="line"><span class="comment">//F0FF23FF --&gt; F 0 FF 23 FF</span></span><br><span class="line"><span class="comment">//说明从左到右开始判断，数字有没优先权</span></span><br><span class="line"></span><br><span class="line">str0 = <span class="string">@&quot;1&quot;</span>;</span><br><span class="line">str1 = <span class="string">@&quot;.1&quot;</span>;</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;%@ compare %@ == %zd&quot;</span>, str0, str1, [str0 compare:str1 options:<span class="built_in">NSNumericSearch</span>]);</span><br><span class="line"><span class="comment">//log : 1 compare .1 == 1</span></span><br><span class="line"><span class="comment">//理解：compare方法内部可以理解为把两个字符串拆分成</span></span><br><span class="line"><span class="comment">//1 --&gt;  0 1</span></span><br><span class="line"><span class="comment">//.1 --&gt; . 1</span></span><br><span class="line"><span class="comment">//空格处应该不补充了一个数字，&quot;.&quot;的ASCII码小于补充数字0的ASCII码。结果等于:1(前者大于后者)</span></span><br><span class="line"></span><br><span class="line">str0 = <span class="string">@&quot;1&quot;</span>;</span><br><span class="line">str1 = <span class="string">@&quot;:1&quot;</span>;</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;%@ compare %@ == %zd&quot;</span>, str0, str1, [str0 compare:str1 options:<span class="built_in">NSNumericSearch</span>]);</span><br><span class="line"><span class="comment">//log : 1 compare :1 == -1</span></span><br><span class="line"><span class="comment">//1 --&gt;  0 1</span></span><br><span class="line"><span class="comment">//.1 --&gt; : 1</span></span><br><span class="line"><span class="comment">//空格处应该不补充了一个数字，&quot;:&quot;的ASCII码大于补充数字0的ASCII码。结果等于:-1(前者小于后者)</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>NSNumericSearch options 会将相邻数字作为一个整体，然后两个字符串的数字整体左对齐，首位补0后进行比较。<br>(以上结论纯属个人理解🙂)</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/NSNumericSearch/" rel="tag"># NSNumericSearch</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2017/01/2017/01/iOS%E5%9F%BA%E7%A1%80%E4%B9%8BnavigationItem.title%E8%AE%BE%E7%BD%AE%E4%BD%86%E4%B8%8D%E6%98%BE%E7%A4%BA/" rel="prev" title="iOS基础之navigationItem.title设置但不显示">
                  <i class="fa fa-chevron-left"></i> iOS基础之navigationItem.title设置但不显示
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2017/05/2017/05/iOS%E5%BC%80%E5%8F%91%E4%B9%8BOC%E9%A1%B9%E7%9B%AE%E6%B7%B7%E7%BC%96Swift%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%B8%AA%E5%9D%91/" rel="next" title="iOS开发之OC项目混编Swift遇到的一个坑">
                  iOS开发之OC项目混编Swift遇到的一个坑 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2015 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vhuichen</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
